package com.niit.spark.sql

import org.apache.spark.sql.{DataFrame, SparkSession}

/**
 * Date:2025/5/20
 * Author：Ys
 * Description:
 */
object Spark_UFD {

  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().appName("Spark_UFD").master("local[*]").getOrCreate()
    spark.sparkContext.setLogLevel("ERROR")

    val df: DataFrame = spark.read.json("input/user.json")
    df.createOrReplaceTempView("user") // 如果想使用SQL语句进行查询，要创建临时视图

    //自定义函数
    spark.udf.register("newName",(name:String)=>{
      "Name:" + name
    } )

    spark.sql("select age,newName(username) from user").show()
    //selectExpr: 使用SQL表达式
    df.selectExpr("age", "newName(username)").show()

    spark.stop()
  }

}
